package com.fastloan.mgt.processcontrol.test;

import com.fastloan.core.apiauth.TestBean;
import com.fastloan.core.apiauth.anno.DecryptParam;
import com.fastloan.core.apiauth.anno.Encrypt;
import com.fastloan.core.apiauth.base.service.impl.ry.model.RYRequestParam;
import com.fastloan.mgt.processcontrol.base.model.ProcessFunction;
import com.fastloan.mgt.processcontrol.base.service.ProcessDispatcher;
import com.fastloan.mgt.processcontrol.base.service.ProcessFunctionService;
import com.fastloan.mgt.product.flow.enums.ProductFunctionGroup;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanwe
 * createTime 2017-11-2017/11/6 19:40
 */
@RestController
@RequestMapping("/api/process")
public class ProcessTestController {

    @Autowired
    private ProcessFunctionService processFunctionService;

    @RequestMapping("/create/{applicationId}")
    public ResultTo create(@PathVariable("applicationId") String applicationId){
        Map<String,Object> map = new HashMap<>();
        map.put("applicationId",applicationId);
        String processId = processFunctionService.createProcess(ProductFunctionGroup.GROUP_ONE,null);
        ProcessDispatcher.getProcessSession(processId).setAttribute("applicationId",applicationId);
        return new ResultTo().setData(processId);
    }

    @RequestMapping("/start/{processId}")
    public ResultTo start(@PathVariable("processId") String processId){
        ProcessDispatcher.startProcess(processId);
        return new ResultTo();
    }

    @RequestMapping("/all")
    public ResultTo getAll(){
        List<ProcessFunction> allProcessFunction = processFunctionService.getAllProcess(ProductFunctionGroup.GROUP_ONE);
        return new ResultTo().setData(allProcessFunction);
    }

    @RequestMapping("/doNext/{processId}")
    public ResultTo doNext(@PathVariable("processId")String processId){
        ProcessDispatcher.doNextFunction(processId);
        return new ResultTo();
    }

    @RequestMapping("/clean")
    public ResultTo clean(){
        processFunctionService.cleanUnfinishedFunction();
        return new ResultTo();
    }

    @RequestMapping("/test")
    @Encrypt
    public Object test(@DecryptParam TestBean object){
        return new ResultTo().setData(object);
    }

}
